package com.frogs.ucenter.service.daoimpl.aop;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.frogs.component.permission.api.service.PermissionHelper;
import com.frogs.component.permission.entity.User;
import com.frogs.system.util.ChangeTypeEnum;
import com.frogs.ucenter.dao.TotalFlowInfoDao;
import com.frogs.ucenter.dao.TotalFlowInfoLogDao;
import com.frogs.ucenter.entity.TotalFlowInfo;
import com.frogs.ucenter.entity.TotalFlowInfoLog;

@Aspect
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("totalFlowInfoLogAopImpl")
public class TotalFlowInfoLogAopImpl {
	@Resource
	private TotalFlowInfoLogDao totalFlowInfoLogDao;
	@Resource
	private TotalFlowInfoDao totalFlowInfoDao;
	@Resource(name = "permissionHelper")
	private PermissionHelper permissionHelper;
	
	private static final Logger log = LoggerFactory.getLogger(TaskInfoLogAopImpl.class);

	
	@AfterReturning(pointcut="execution(* com.frogs.ucenter.controller.TotalFlowInfoController.saveTotalFlowInfo(..)) || "
			+ "execution(* com.frogs.ucenter.controller.TotalFlowInfoController.updateTotalFlowInfo(..)) || "
			+ "execution(* com.frogs.ucenter.controller.TotalFlowInfoController.deleteTotalFlowInfo(..))")
	public void doAfterReturning(JoinPoint jp)
	{
		try {
			HttpServletRequest req = (HttpServletRequest)jp.getArgs()[0];//切入点方法入参
			boolean flag = (Boolean) req.getAttribute("flag");
			String method = jp.getStaticPart().getSignature().getName();
			if(!flag){
				return;
			}
			TotalFlowInfo totalFlowInfo = new TotalFlowInfo();
			int changeType = 0;
			User user = permissionHelper.getUser();
			int userId = user.getUserId();
			if(method.equals("saveTotalFlowInfo")){
				totalFlowInfo = (TotalFlowInfo)jp.getArgs()[3];
				changeType = ChangeTypeEnum.ADD.getId();
				insertLog(totalFlowInfo, changeType, userId);
			}else if(method.equals("updateTotalFlowInfo")){
				totalFlowInfo = (TotalFlowInfo)jp.getArgs()[3];
				changeType = ChangeTypeEnum.UPDATE.getId();
				insertLog(totalFlowInfo, changeType, userId);
			}else if(method.equals("deleteTotalFlowInfo")){
				String deleteId = jp.getArgs()[3].toString();
				String[] arr = deleteId.split(",");
				changeType = ChangeTypeEnum.DELETE.getId();
				for(int i=0;i<arr.length;i++){
					totalFlowInfo = new TotalFlowInfo();
					totalFlowInfo.setId(Integer.parseInt(arr[i]));
					insertLog(totalFlowInfo, changeType, userId);
				}
			}
		} catch (Exception e) {
			log.error("func[doAfterReturning] jp[" + jp + "] error[" + e.getMessage() + "]", e);
		}
		
	}
	
	@Async
	private void insertLog(TotalFlowInfo totalFlowInfo, int changeType, int creater){
		TotalFlowInfoLog totalFlowInfoLog = new TotalFlowInfoLog();
		totalFlowInfoLog.setApplicablePlatformId(totalFlowInfo.getApplicablePlatformId());
		totalFlowInfoLog.setChangeType(changeType);
		totalFlowInfoLog.setCreater(creater);
		totalFlowInfoLog.setEndTime(totalFlowInfo.getEndTime());
		totalFlowInfoLog.setExpandCode(totalFlowInfo.getExpandCode());
		totalFlowInfoLog.setLevelId(totalFlowInfo.getLevelId());
		totalFlowInfoLog.setOldId(totalFlowInfo.getId());
		totalFlowInfoLog.setRemark(totalFlowInfo.getRemark());
		totalFlowInfoLog.setStartTime(totalFlowInfo.getStartTime());
		totalFlowInfoLog.setTotalFlowName(totalFlowInfo.getTotalFlowName());
		totalFlowInfoLog.setUrl(totalFlowInfo.getUrl());
		totalFlowInfoLogDao.insert(totalFlowInfoLog);
	}
}
